// 入驻申请
<template>
   <div class="search">
     <Row>
        <Card>
          <Row class="operation">
            <Button @click="getDataList" icon="md-refresh">刷新</Button>
            <Button @click="add" icon="md-add" type="primary">合同上传</Button>
            <Button @click="delAll" icon="md-trash" type="default">批量删除</Button>
          </Row>

          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort" @on-selection-change="changeSelection"></Table>
          </Row>

          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
     </Row>

     <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="600">
      <Form ref="form" :model="form" :label-width="160" :rules="formValidate" >
        <FormItem label="服务分类" prop="goodCategoryId">
          <Select v-model="form.goodCategoryId" style="width: 100%">
            <Option v-for="item in classData" :key="item.id" :value="item.id">{{item.title}}</Option>
          </Select>
        </FormItem>
        <FormItem label="合同标题" prop="docTitle">
          <Input v-model="form.docTitle" style="width: 100%" />
        </FormItem>
        <FormItem label="合同公网下载地址" prop="docUrl">
          <upload-input-pdf v-model="form.docUrl" style="width: 100%"></upload-input-pdf>
        </FormItem>
        <FormItem label="上传数量" prop="uploadCount">
          <InputNumber v-model="form.uploadCount" style="width：100%"></InputNumber>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>

   </div> 
</template>

<script>
import { validateName } from "@/libs/validate";
import { uploadFile } from "@/api/index";
import uploadInputPdf from "@/views/my-components/xboot/upload-input-pdf"

export default {
  components: {
    uploadInputPdf
  },
  data(){
    return{
      loading: false,
      submitLoading: false,
      total:0,
      modalTitle: "",
      modalVisible: false,
      data:[],//表单数据
      searchForm:{
        pageSize:10,
        pageNumber:1,
      },
      form: {
        id: "",
        goodCategoryId: "",
        uploadCount: 0,
        docTitle: "",
        docUrl: "",
      },
      formValidate: {
        uploadCount: [{ required: true, message: "请输入上传数量", trigger: "blur", type: "number" }],
        goodCategoryId: [{ required: true, message: "请选择服务分类", trigger: "change" }],
        docTitle: [{ required: true, message: "请输入合同标题", trigger: "blur" },
        { validator: validateName, trigger: "blur" }],
        docUrl: [{ required: true, message: "请输入合同公网下载地址", trigger: "blur" }],
      },
      columns: [
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "合同编号",
          key: "contractId",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "合同标题",
          key: "docTitle",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "所属分类",
          key: "goodCategoryName",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "合同",
          key: "docUrl",
          minWidth: 120,
          sortable: false,
          render: (h,params)=> {
            return h("img", {
              attrs: {
                src: "https://fanshafuoss.oss-cn-beijing.aliyuncs.com/df20c7b5b5bd45ca95fb5061d52881f3.jpg",
              },
              style: {
                width: "70px",
                height: "70px"
              },
              on: {
                click: ()=> {
                  window.open(params.row.docUrl)
                }
              }
            })
          }
        },
        {
          title: "创建时间",
          key: "createTime",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "操作",
          width: 360,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    "marginRight": "5px"
                  },
                  on: {
                    click: () => {
                      this.showContract(params.row);
                    }
                  }
                },
                "查看合同"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.removeContract(params.row);
                    }
                  }
                },
                "移除合同"
              ),
            ]);
          }
        }
      ],
      classData: [],
      selectData: [],
      selelctCount: 0,
      }
    },
    methods:{
      init(){
        this.getDataList();
        this.getClassList();
      },
      changeSelection (val) {
        console.log(val);
        this.selectData = val;
        this.selelctCount = val.length;
      },
      delAll () {
        this.$Modal.confirm({
          title: "删除",
          content: "确认删除所选的"+this.selelctCount+"条数据吗？",
          loading: true,
          onOk: ()=> {
            var array = [];
            this.selectData.forEach(item=> {
              array.push(item.id);
            })
            var ids = array.join(",");
            // console.log(ids)
            var data = {
              ids: ids
            }
            this.postRequest("/electronicContract/removeElectronicContract", data).then(res=> {
              this.loading = false;
              if (res.success) {
                this.$Message.success("移除成功");
                this.getDataList();
                this.$Modal.remove()
              } else {
                this.$Message.error(res.message);
                this.getDataList();
                this.$Modal.remove()
              }
            })
          }
        })
      },
      getClassList () {
        this.postRequest("/goodCategory/getGoodServiceGoodCategory").then(res=> {
          if (res.success) {
            this.classData = res.result;
          } else {
            this.classData = [];
          }
        })
      },
      showContract (v) {
        var data = {
          id: v.id
        }
        this.postRequest("/electronicContract/viewContract",data).then(res=> {
          if (res.success) {
              window.open(res.result)
          } else {
            this.$Message.error(res.message)
          }
        })
      },

      removeContract (v) {
        this.$Modal.confirm({
          title: "移除合同",
          content: "确定要移除该合同吗？",
          loading: true,
          onOk: ()=> {
            var data = {
              ids: v.id
            }
            this.postRequest("/electronicContract/removeElectronicContract", data).then(res=> {
              this.loading = false;
              if (res.success) {
                this.$Message.success("移除成功");
                this.getDataList();
                this.$Modal.remove()
              } else {
                this.$Message.error(res.message);
                this.getDataList();
                this.$Modal.remove()
              }
            })
          }
        })
      },

      getDataList(){
        this.loading = true;
        this.postRequest("/electronicContract/getElectronicContractList",this.searchForm).then(res =>{
            this.loading = false
            if(res.success){
                var arr = res.result.dataResult;
                for(let val of arr){
                  for(let i in val){
                    (val[i] === null) ? (val[i] = "-") : ""
                  }
                }
                this.data = arr;
                this.total = res.result.totalSize;
            } else {
                this.data = [];
                this.total = 0;
            }
        })
      },
      changeSort(e) {
        this.searchForm.sort = e.key;
        this.searchForm.order = e.order;
        if (e.order === "normal") {
          this.searchForm.order = "";
        }
        this.getDataList();
      },
      changePage(v) {
        this.searchForm.pageNumber = v;
        this.getDataList();
      },
      changePageSize(v) {
        this.searchForm.pageSize = v;
        this.getDataList();
      },
      clearSelectAll(){
        this.$refs.searchForm.resetFields()
        this.getDataList()
      },
      add () {
        this.modalTitle = "添加";
        this.$refs.form.resetFields();
        this.modalVisible = true;
      },
      handleSubmit () {
        this.$refs.form.validate(valid=> {
          if (valid) {
            this.submitLoading = true;
            delete this.form.id;
            this.postRequest("/electronicContract/insertElectronicContract", this.form).then(res=> {
            this.submitLoading = false;
              if (res.success) {
                this.$Message.success("添加成功");
                this.getDataList();
                this.modalVisible = false;
              } else {
                this.$Message.success(res.message);
                this.modalVisible = false;
              }
            })
          }
        })
      },
      remove (v) {
        this.$Modal.confirm({
          title: "删除",
          content: "确认删除所选的视频数据吗？",
          loading: true,
          onOk: ()=> {
            var data = {
              ids: v.id
            }
            this.postRequest("/shopWorks/removeShopWorks", data).then(res=> {
              this.loading = false;
              if (res.success) {
                this.$Message.success("删除成功");
                this.getDataList();
                this.$Modal.remove();
              } else {
                this.$Message.success(res.message);
                this.$Modal.remove();
              }
            })
          }
        })
      }
    },
    mounted(){
      this.init()
    }
}
</script>

<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>